/*
 * @Author: duanguang
 * @Date: 2021-03-31 17:21:01
 * @LastEditTime: 2021-04-20 16:20:31
 * @LastEditors: duanguang
 * @Description: 
 * @FilePath: /legions-Luban/client/src/home/stores/projectCommonStore.ts
 * 「扫去窗上的尘埃，才可以看到窗外的美景。」
 */
import StoreBase, { IStoreBaseMeta } from '../../common/stores/StoreBase';
import { observable, autorun, action, computed } from 'mobx';
import UiStoreBase from '../../common/stores/ui/UiStoreBase';
import {  StoreModules } from 'legions/store';
import { InstanceProForm } from 'legions-pro-design/es/LegionsProForm/interface';
import { InstanceProModal } from 'legions-pro-design/es/LegionsProModal/interface';
import type { ISelectAutoQuery } from 'legions-pro-design/es/LegionsStoreConditions/interface';
import { InstanceProTable } from 'legions-pro-design/es/LegionsProTable/interface';
import { observableViewModel } from 'legions/store-utils';
import ProjectListPagePropertyStore from './projectListPageProperty';
import { ViewModel } from 'brain-store-utils';
import { ComponentViewResource, IResourceEvent, ITriggerEventPrams } from './even/resourceEvent';
import { PropertyKeyValueFormFields } from '../models/form/propertyFormModel';
import has from 'lodash/has';
import get from 'lodash/get';
import set from 'lodash/set';
type Proxify<T> = { [P in keyof T]: T[P] };
interface IContext {
  listPageApp:ProjectListPagePropertyStore
}
interface IProjectCommonFormRef {
  addSelLocalListTableForm: {
    table: InstanceProTable;
    form: InstanceProForm;
  };
  addSelAutoQueryForm: InstanceProForm;
}
interface IEnentDataSource{
  /** 事件名称 */
  eventName: string;
  /** 事件内容 */
  value: string;
}
export type typeSelectValueData={
  labelKey: string;
  labelValue: string;
  label?: string;
}
class SelectVModel{
  constructor(uid: string) {
    this.componentUid = uid;
  }
   /** 添加下拉选项组件UI数据 */
  @observable selectValueData: typeSelectValueData[] = [];

  /** 远程数据 */
  @observable remoteData: ISelectAutoQuery = null;
  @observable componentUid = '';
  /** 组件使用范围，例如在搜索组件，或者表单组件 */
  @observable useScope: 'proCondition' | 'proForm' = null;
  /** 数据类型 */
  @observable dataType: 'local' | 'remote' = null;
}

@StoreModules
export default class ProjectCommonStore extends UiStoreBase<IContext> {
  static meta: IStoreBaseMeta = {
    ...StoreBase.meta,
    eventScopes: [],
    contextTypes: {
      listPageApp:ProjectListPagePropertyStore
    },
  };
  constructor(context: IContext) {
    super(context);
  }
  /** 公共表单组件实例 */
  projectCommonFormRef: IProjectCommonFormRef = {
    /** 下拉选项添加本地值表单实例 */
    addSelLocalListTableForm: {
      table: null,
      form: null,
    },
    /** 下拉选项远程数据表单配置示例 */
    addSelAutoQueryForm: null,
  };
   /** 
   * 下拉选项值设置模态框实例(内容区域数据List和远程接口配置表单)
   * path:'components.widget.addSelectOptions'
   */
  formSelectValueModalRef: InstanceProModal = null;

  /** 添加下拉选项编辑代码表达式模态框实例 *  * path:'components.widget.addSelectOptions'*/
  formAddKeyValueCodeExpressionModalRef: InstanceProModal = null;

   /** 事件信息Table数据展示模态框实例 */
  formAddEventListTableModalRef: InstanceProModal = null;

  /** 事件信息列表数据Table组件实例 */
  formAddEventListTableRef: InstanceProTable = null;

  /** 添加事件编辑代码表达式模态框实例 */
  formAddEventCodeExpressionModalRef: InstanceProModal = null;
  @observable selectVModel = observable.map<string,ViewModel<SelectVModel> & Proxify<SelectVModel>>();

  /** 属性正在编辑状态的组件uid */
  @observable editComponentUid = '';

  /** 添加的事件数据源 */
  @observable evenDataSource = observable.map<string,IEnentDataSource[]>()
  @computed get computedSelectVModel() {
    const uid = this.editComponentUid;
    return this.selectVModel.get(uid)
  }
  /** 搜索条件下拉数据项集合 */
  @computed get computedConditionSelectOpt() {
    const result: Proxify<SelectVModel>[] = [];
    for (let value of this.selectVModel.values()) {
      if (value.useScope === 'proCondition') {
        result.push(value);
      }
    }
    return result;
  }
  /** 筛选指定组件事件数据 */
  @computed get computedEventList() {
    if (this.evenDataSource.has(this.editComponentUid)) {
      return this.evenDataSource.get(this.editComponentUid)
    }
    return [];
  }
  /** 添加下拉选择器数据
   * @param {SelectVModel['useScope']} useScope 组件使用范围，例如在搜索组件，或者表单组件
   * @param  {SelectVModel['dataType']} dataType 
   * @memberof ProjectCommonStore
   */
  @action addSelectValue(useScope:SelectVModel['useScope'],dataType:SelectVModel['dataType'],options: {
    local?: typeSelectValueData[];
    remote?:ISelectAutoQuery
  }) {
    const uid = this.editComponentUid;
    const viewModel = observableViewModel<SelectVModel>(new SelectVModel(uid));
    viewModel.useScope = useScope;
    viewModel.dataType = dataType;
    if (dataType === 'local') {
      viewModel.selectValueData = options.local || [];
    }
    if (dataType === 'remote') {
      viewModel.remoteData = options.remote;
    }
    this.selectVModel.set(uid,viewModel);
  }

  /** 更新下拉选择数据
   * @param {SelectVModel['useScope']} useScope
   * @param {SelectVModel['dataType']} dataType
   * @memberof ProjectCommonStore
   */
  @action updateSelectValue(dataType: SelectVModel['dataType'],options: {
    local?: typeSelectValueData[];
    remote?: ISelectAutoQuery;
    useScope?: SelectVModel['useScope'];
  }) {
    const uid = this.editComponentUid;
    if (this.selectVModel.has(uid)) {
      const viewModel = this.selectVModel.get(uid);
      viewModel.dataType = dataType;
      if (options.useScope) {
        viewModel.useScope = options.useScope;
      }
      if (dataType === 'local') {
        viewModel.selectValueData = options.local || [];
      }
      if (dataType === 'remote') {
        viewModel.remoteData = options.remote;
      }
      this.selectVModel.set(uid,viewModel);
    }
    else {
      this.addSelectValue(options.useScope,dataType,options);
    }
  }
  /** 变更正在活动组件的UID信息*/
  @action modifyActivityComponentUid(uid:string) {
    this.editComponentUid = uid;
  }
  /** 下拉选择器组件配置参数-表单数据 */
  getAddSelectValueFormPropertyValue(field: string) {
    const formvalue: PropertyKeyValueFormFields = this.projectCommonFormRef.addSelAutoQueryForm
      .viewModel.InputDataModel as PropertyKeyValueFormFields;
    return get(formvalue, `${field}.value`);
  }
  /** 更新事件数据数据源信息 */
  @action updateEventDataSource(eventList:Array<IEnentDataSource>) {
    this.evenDataSource.set(this.editComponentUid,eventList);
  }
}
